<%@ page language="java"  pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
      	<style type="text/css">
		* {
			font-size: 12px;
		}
    	</style>
		<link rel="stylesheet" type="text/css" href="<%=basePath%>js/jquery/themes/default/easyui.css">
		<link rel="stylesheet" type="text/css" href="<%=basePath%>js/jquery/themes/icon.css">
		<script type="text/javascript" src="<%=basePath%>js/jquery/jquery-1.7.2.min.js"></script>
		<script type="text/javascript" src="<%=basePath%>js/jquery/jquery.easyui.min.js"></script>
		<script type="text/javascript" src="<%=basePath%>js/jquery/locale/easyui-lang-zh_CN.js"></script>
 		<script type="text/javascript" src="<%=basePath%>js/ztree/js/jquery.ztree.core-3.5.js"></script>
 		<script type="text/javascript" src="<%=basePath%>js/ztree/js/jquery.ztree.excheck-3.5.js"></script>
 	    <link rel="stylesheet" href="<%=basePath%>js/ztree/css/zTreeStyle/zTreeStyle.css" type="text/css">
 		<script type="text/javascript">
 		var pid = -10 ;
 		var pname = "" ;			
 		var vailflag = false ;
 		var editflag = false;
 		function oncheckClick()
 		{
 			var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
			var nodes = treeObj.getCheckedNodes(true);
			var id = nodes[0].id ;
			pid = id ;
			pname = nodes[0].name ;
			var queryParams = $('#dataGrid').datagrid('options').queryParams;  
			queryParams.pid = id;
			$('#dataGrid').datagrid({
				queryParams:queryParams,
				url :"<%=basePath %>user/getUserListByDeptId.xhtml"
			});
 		}
		var setting = {
			async: {
				enable: true,
				url: "<%=basePath %>dept/getAllDept.xhtml",
				autoParam: ["id"]
			},
			check: {
				enable: true,
				chkStyle: "radio",
				radioType: "all"
			},
			callback:{
				onCheck :oncheckClick
			},
			data: {
				simpleData: {
					idKey: "id",
					pIdKey:"pid",
					enable: true
				}
			}
		};
		
		var code;		
		function setCheck() {
			var type = $("#level").attr("checked")? "level":"all";
			setting.check.radioType = type;
			showCode('setting.check.radioType = "' + type + '";');
			$.fn.zTree.init($("#treeDemo"), setting, null);
		}
		function showCode(str) {
			if (!code) code = $("#code");
			code.empty();
			code.append("<li>"+str+"</li>");
		}
		
		$(document).ready(function(){
			setCheck();			
		});
 		</script>
 	<script type="text/javascript">
 		function sub(){
 			if($('#ff').form('validate')){
				$('#ff').submit();				
			}
		
		}
		
		function closeW(){
			$('#logicWindow').window('close');
		}
		
		function showEdit(){
			var rows = $('#dataGrid').datagrid('getSelections');
			if(rows.length != 1)
			{
				$.messager.alert('提示','请选中一条记录');
			}else {
				$('#ff').form({
				onSubmit:function (){
					var t =  $(this).form('validate');
					if(t)
					{
						$('#btnsub').linkbutton({disabled:true});
					}
					return t;
				},
				success:function(data){
					var json = eval("("+data+")");
					if(!json.state)
					{
						alert(json.msg);
					}
					$('#btnsub').linkbutton({disabled:false});
					closeW();
					$("#dataGrid").datagrid("reload");
				}
			});
				var now=new Date();
				var number = now.getYear().toString()+now.getMonth().toString()+now.getDate().toString()+now.getHours().toString()+now.getMinutes().toString()+now.getSeconds().toString();
		
				$('#ff').form('load','<%=basePath%>sys/user/userAction!load.action?userEntity.id='+rows[0].id+'&t='+number);
         
				$('#logicWindow').window('open');
				
					
				
                if(document.getElementById("deviceId").value!=null){
             
                	document.getElementById("d1").style.display="block";
		 			document.getElementById("deviceId").style.display="block";
                }else if(document.getElementById("deviceId").value==null){
                	document.getElementById("d1").style.display="none";
		 			document.getElementById("deviceId").style.display="none";
                }
				
			}
			
			document.getElementById('ff').action = '<%=basePath%>sys/user/userAction!update.action' ;
		}
		
		
		
		
		function deleteLogic()
		{
			var rows = $('#dataGrid').datagrid('getSelections');
			if(rows.length >0 )
			{
				var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
				
				var ids = []; 
				for (var i = 0 ; i < rows.length ; i ++)
				{
/*				
					var nodes = treeObj.getNodesByParam("pid", rows[i].id, null);
					if(nodes.length != 0)
					{
						alert('选中的部门还存在子部门,不允许删除');
						return ;
					}
					*/
					ids.push(rows[i].id);
				}
				var now=new Date();
				var number = now.getYear().toString()+now.getMonth().toString()+now.getDate().toString()+now.getHours().toString()+now.getMinutes().toString()+now.getSeconds().toString();
		
				$.ajax({
					url:"<%=basePath%>sys/user/userAction!delete.action?deleteid="+ids.join(","),
					success:function (data){
					var json = eval("("+data+")");
						if(!json.state)
						{
							alert(json.msg);
						}else {
							$("#dataGrid").datagrid("reload");
						}
					}
				});
			}else {
				$.messager.alert('提示','请选择需要删除的数据');
			}
		
		}
 			$(document).ready(function (){
			$('#dataGrid').datagrid({
				width:"auto",
				pageSize:20,
				title:"用户管理",
				iconCls:'icon-save',
				pagination:true,
				striped:true,
				loadMsg:"加载中...",
				rownumbers:true,
				collapsible:true,
				fit:true,
				frozenColumns:[[ 
                {field:'id',checkbox:true}
				]], 
				toolbar:[{
					id:"btnadd",
					text:"新增用户",
					iconCls: "icon-add",
					handler:function ()
					{
						if(pid == -10)
						{
							alert('请选择部门树');
							return ;
						}else {
							document.getElementById("lastProduct").innerHTML = pname;
						
						addW();
						}
						
					}
				},{
					id:"btnedit",
					text:"修改用户信息",
					iconCls: "icon-edit",
					handler:function()
					{
						showEdit();
					}
				},{
					id:"btndelete",
					text:"删除用户",
					iconCls: "icon-remove",
					handler:function()
					{
						deleteLogic();
					}
				},{
					id:"btnrole",
					text:"分配角色",
					iconCls:"icon-ok",
					handler:function()
					{
						roleUserOpen();
					}
				},{
					id:"btnrole",
					text:"部门调换",
					iconCls:"icon-ok",
					handler:function()
					{
						replaceDep();
					}
				}],
				columns:[[
					{field:"username",title:"用户名",width:"150"},
					{field:"sex",title:"性别",width:"150"},
					{field:"truename",title:"真实姓名",width:"150"},
					{field:"tel",title:"电话",width:"150"},
					{field:"email",title:"邮箱",width:"150"},
					{field:"ismobileno",title:"是否允许手机登陆",width:"150",formatter:function (d,t){
					if(d==0) return '是';
					if(d == 1) return '否';
					}},
					{field:"deptid",title:"部门ID",width:"150",hidden:"true"}
				]]
			});	
			
			initRoleGrid();
			});	

			
			function roleUserOpen()
			{
				var rows = $('#dataGrid').datagrid('getSelections'); 
				if(rows.length == 1)
				{
					
					$('#roleUserGrid').datagrid({
						url:"<%=basePath%>sys/role/roleAction!getRoleListString.action?userstate=1&userid="+rows[0].id
					});
					$('#roleWindow').window('open');
				}else {
					alert('请选择一条记录');
				}
			}
			
			
			function addW()
			{
					document.getElementById("username").disabled=false;
					editflag = true;
					$('#userSpan').html('');
					$('#ff').form('clear');
					document.getElementById("deptid").value = pid;
					$('#ff').form({
						onSubmit:function (){
							var t =  $(this).form('validate');
							if(t)
							{
								$('#addbtnsub').linkbutton({disabled:true});
							}
							return t;
						},
						success:function(data){
							var json = eval("("+data+")");
							if(!json.state)
							{
								alert(json.msg);
							}else {
								setCheck();
							}
							$('#addbtnsub').linkbutton({disabled:false});
							closeW();
							$("#dataGrid").datagrid("reload");
						}
					});
					document.getElementById('ff').action = '<%=basePath%>user/addUser.xhtml' ;
					$('#logicWindow').window('open');
			}
			
			function initRoleGrid()
			{
				$('#roleUserGrid').datagrid({
				
				width:"auto",
				title:"角色管理",
				iconCls:'icon-save',
				striped:true,
				loadMsg:"加载中...",
				rownumbers:true,
				collapsible:true,
				frozenColumns:[[ 
                {field:'roleid',width:'30',formatter:function (v,c){ 
                var ck = ""; if(Boolean(c.checked)){ck = "checked"}
                else {}; return '<input type="checkbox" name="ckAttri" '+ck+'  value='+v+'>';  } } ]],
				columns:[[
					{field:"rolename",title:"角色名称",width:"300"} 
				]]
			});	
			}
			
			function userRole()
			{
				var ckAttriargs = document.getElementsByName("ckAttri");
				var ckid = [];
				for(var i = 0 ; i < ckAttriargs.length ; i ++)
				{
					if(ckAttriargs[i].checked)
					{
						ckid.push(ckAttriargs[i].value);
					}
					
				}
				var rows = $('#dataGrid').datagrid('getSelections'); 
				var ckids = ckid.join(",");
				var userid = rows[0].id;
				$.ajax({
					type:"post",
					url:"<%=basePath %>sys/user/userAction!saveUserRole.action",
					data:{"userid":userid,"roleids":ckids},
					success:function (data){
						var json = eval("("+data+")");
							if(!json.state)
							{
								alert(json.msg);
							}else {
								alert('保存成功');
								 closeRW();
							}
					}
				});
			}
			
			function closeRW()
			{
				$('#roleWindow').window('close');
			}
			
			function searchForm()
			{
				var searchusername = document.getElementById("searchname").value ;
				var searchtruename = document.getElementById("searchtruename").value ;
				var queryParams = $('#dataGrid').datagrid('options').queryParams;  
				queryParams.username = searchusername;
				queryParams.truename = searchtruename;
				$('#dataGrid').datagrid({
					queryParams:queryParams
				});
				$('#dataGrid').datagrid("reload");
			}
			
			function clearForm()
			{
				document.getElementById("searchname").value = "" ;
				document.getElementById("searchtruename").value = "" ;
			}
			function validAjaxUsername(obj)
			{
				if(!obj.value || obj.value == '')
				{
					
				}else if(editflag){
					vailflag=true;
				}else{
				$.ajax({
					type:"post",
					data:{"searchusername":obj.value},
					url:"<%=basePath %>sys/user/userAction!validUsername.action",
					success:function (data){
						if(data > 0)
						{
							$('#userSpan').html('<font color="red">已存在的用户名</font>');
							document.getElementById("username").focus();
							vailflag = false ;
						}else if(data == 0){
							$('#userSpan').html('');
							vailflag = true ;
						}
					}
				});				
				}

			}
			
			
			function isselect(){
			
			 var r=document.getElementById("ismobileno").value;
			 	if(r==0){
			 		document.getElementById("d1").style.display="block";
			 		document.getElementById("deviceId_td").style.display="block";
			 	}else if(r==1){
			 		document.getElementById("d1").style.display="none";
		 			document.getElementById("deviceId_td").style.display="none";
			 	}
			}
			
			
			
				
			function replaceDep(){

				var rows = $('#dataGrid').datagrid('getSelections'); 
				if(rows.length > 0)
				{
					setCheck2();
					$('#replaceDepWindow').window('open');
					var treeObj = $.fn.zTree.getZTreeObj("treeDemo2");
					treeObj.expandAll(true); 
					
				}else {
					$.messager.alert('提示','请选择人员');
				}
			}		
			
		function replaceDep8(){
		 	var treeObj = $.fn.zTree.getZTreeObj("treeDemo2");
			var nodes = treeObj.getCheckedNodes(true);
			var deptid = nodes[0].id ;
			var name = nodes[0].name;
			var rows = $('#dataGrid').datagrid('getSelections'); 
			
			var ids = []; 
			for (var i = 0 ; i < rows.length ; i ++){
				ids.push(rows[i].id);
			}
			
				if(rows.length >0){
					$.ajax({
						type: "POST",
					    async: true,
					    cache: false,
					    url: "<%=basePath%>sys/role/roleAction!replaceDep.action?rand="+Math.random(),
					    data: {"userid":ids.join(","),"deptid":deptid},
					    success:function(d){
					    		if("true"==d){
					    			$("#dataGrid").datagrid("reload");
					    			$('#replaceDepWindow').window('close');
						    	}else{
						    		$.messager.alert('提示',"调换部门失败");
						    	}
						}
				    });
				}else {
					$.messager.alert('提示','请选择人员');
				}

			
		}
		
		var setting2 = {
			async: {
				enable: true,
				url: "<%=basePath %>sys/dept/deptAction!getDeptJson.action",
				autoParam: ["id"]
			},
			check: {
				enable: true,
				chkStyle: "radio",
				radioType: "all",
				chkboxType: {
				"Y": "ps",
				"N": "ps"
				}
			},
			callback:{
				onCheck :replaceDep8
			},
			data: {
				simpleData: {
					idKey: "id",
					pIdKey:"pid",
					enable: true
				}
			}
		};
		
		var code2;		
		function setCheck2() {
			var type = $("#level").attr("checked")? "level":"all";
			setting.check.radioType = type;
			showCode('setting.check.radioType = "' + type + '";');
			$.fn.zTree.init($("#treeDemo2"), setting2, null);
				var treeObj = $.fn.zTree.getZTreeObj("treeDemo2");
					treeObj.expandAll(true); 
		}
		function showCode2(str) {
			if (!code2) code2 = $("#code");
			code2.empty();
			code2.append("<li>"+str+"</li>");
		}
 		</script>
  </head>
  
  <body class="easyui-layout" >
  		<div region="west" split="true" title="用户管理" style="width: 200px; padding: 1px;">
			<ul id="treeDemo" class="ztree"></ul>
		</div>
		
		<div region="center" split="true" title="用户管理信息">
				<div class = "easyui-layout" fit="true" >
					<div region = "center" >
						<table id = "dataGrid"  ></table>
					</div>
					<div region = "north" style = "height: 45px;" >
						<form action="" id = "searchForm" >
							<table>
								<tr>
									<td>用户名:</td>
									<td><input type = "text" id = "searchname" /></td>
									<td>真实姓名:</td>
									<td><input type = "text" id = "searchtruename" /></td>
									<td><a  class = "easyui-linkbutton" iconCls = "icon-search" onclick = "searchForm()" >搜 索</a></td>
									<td><a  class = "easyui-linkbutton" iconCls = "icon-reload" onclick = "clearForm()" >清 空</a></td>
								</tr>
							</table>
						</form>
					</div>
				</div>
		</div>
		
			<div id="logicWindow" class="easyui-window" title="用户操作" iconCls="icon-save" style="width: 500px; height: 300px; padding: 5px;" closed = "true"  >
			<div class="easyui-layout" fit="true">
				<div region="center" border="false"
					style="background: #fff; border: 1px solid #ccc;">
					<form id="ff" method="post">
						
						<input type="hidden" name="deptid" id="deptid">
						<input type="hidden" name="id" id="id">
						<table >

							<tr>
								<td >
									<label for="classname">
										所属部门:
									</label>
								</td>
								<td colspan="3"  >
									<span id = "lastProduct" ></span>
								</td>
							</tr>
							<tr>
								<td>
									<label for="classname">
										用户名:
									</label>
								</td>
								<td colspan="3" >
									<input disabled="disabled"  class="easyui-validatebox"  onblur="validAjaxUsername(this)"  validType="length[0,20]"  type="text" style="width: 200" id="username" name="username" required="true"></input>
									<span id = "userSpan" ></span>
								</td>
							</tr>
							<tr>
								<td>
									<label for="classname">
										是否允许手机登陆:
									</label>
								</td>
								<td >
									<select class="easyui-combobox" id="ismobileno"  name = "ismobileno" required="true"  data-options = "onSelect:isselect"   >
										<option value="0">是</option>
										<option value="1">否</option>
									</select>
									
								</td>
									<td style="display:none" id="d1">
									<label for="classname">
										手机标示码:
									</label>
								</td>
								<td style="display:none" id = "deviceId_td" >
									<input class="easyui-validatebox" type="text" style="width:80" id="deviceId" name="deviceId" ></input>
								</td>
							</tr>
								<tr>
								<td>
									<label for="classname">
										性别:
									</label>
								</td>
								<td colspan="3"  >
									<select class = "easyui-combobox" name="sex"   >
										<option value = "男" >男</option>
										<option value = "女" >女</option>
										
									</select>
								</td>
							
							</tr>
								<tr>
								<td>
									<label for="classname">
										真实姓名:
									</label>
								</td>
								<td colspan="3"  >
									<input class="easyui-validatebox" type="text" style="width: 200" id="truename" name="truename" required="true"></input>
								</td>
							</tr>
														<tr>
								<td>
									<label for="classname">
										电话:
									</label>
								</td>
								<td colspan="3"  >
									<input class="easyui-validatebox" type="text" style="width: 200" id="tel" name="tel" required="true"></input>
								</td>
							</tr>
							
														<tr>
								<td>
									<label for="classname" >
										邮箱:
									</label>
								</td>
								<td colspan="3" >
									<input class="easyui-validatebox" validType="email" type="text" style="width: 200" id="email" name="email" ></input>
								</td>
							</tr>
						</table>
				
					</form>
				</div>
		<div region="south" border="false"
					style="text-align: right; padding: 5px 0;">
					<a class="easyui-linkbutton" id="addbtnsub" iconCls="icon-ok"
						onclick = "sub()">提 交</a>
					<a class="easyui-linkbutton" iconCls="icon-cancel"
						onclick = "closeW()">关闭</a>
				</div>
	
	
			<div id="roleWindow" class="easyui-window" title="用户操作" modal = "true" iconCls="icon-save" style="width: 500px; height: 500px; padding: 5px;" closed = "true"  >
					<div>
						<div region = "center" >
							<table id = "roleUserGrid"  ></table>
						</div>
						<div region="south" border="false" style="text-align: right; padding: 5px 0;" >
							<a class="easyui-linkbutton" id="addbtnsub" iconCls="icon-ok"
								onclick = "userRole()">提 交</a>
							<a class="easyui-linkbutton" iconCls="icon-cancel"
								onclick = "closeRW()">关闭</a>
						</div>
					</div>
			</div>
			
			
			<div id="replaceDepWindow" class="easyui-window" title="部门调换" modal = "true" iconCls="icon-save" style="width: 250px; height: 500px; padding: 5px;" closed = "true"  >
					<div>
							<ul id="treeDemo2" class="ztree"></ul>
					</div>
			</div>
			
  </body>
</html>
